package com.digitalxpert.jargle.TestGui;
import java.text.ParseException;
import java.util.regex.Pattern;
import javax.swing.JFormattedTextField;
import javax.swing.text.DefaultFormatter;

/**
 * Class InnFormatter
 * 
 * @author Yerokhin Yuri copyright by Zsoft Company
 * @version 1.0
 */
public class NumberFieldMaskFormatter extends DefaultFormatter{
    private Pattern mask = Pattern.compile("(\\d{0,100}(%)?)");
    private JFormattedTextField tf = null;
    public NumberFieldMaskFormatter(){
        setOverwriteMode(false);
    }
    public void install(JFormattedTextField ftf){
        super.install(ftf);
        tf = ftf;
    }
    public Object stringToValue(String string) throws ParseException{
        String value = string.trim();
        if(!mask.matcher(string).matches()){
            value = (String) tf.getValue();
            tf.setValue(value);
        }else{
            if(tf != null)
                tf.setValue(value);
        }
        return value;
    }
    public String valueToString(Object value) throws ParseException{
        if(value == null)
            return "";
        return value.toString();
    }
}
